package de.tabuma.projectview.navbar;

import de.tabuma.projectview.*;
import de.tabuma.pm.gadget.entity.*;
import de.tabuma.pm.gadget.logik.*;
import de.tabuma.pm.externalcontent.entity.*;
import de.tabuma.pm.externalcontent.logik.*;
import de.tabuma.pm.externalproject.entity.*;
import de.tabuma.pm.externalproject.logik.*;
import java.util.regex.*;
import java.util.*;


/**
 *
 * @author alefen
 */
public class GadgetFacade {

    public static String getGadget(Gadget gadget) {
        String str = "";
        str += "<div class=\""+constants.gadget + gadget.getId()+"\" id=\""+constants.gadget + gadget.getId()+"\">\n";
        str += "<iframe class=\""+constants.gadgetIframe + gadget.getId()+"\" name=\"" + constants.gadgetIframe + gadget.getId() + "\"></iframe>\n";
        str += "</div>\n";
        return str;
    }

    public static String getPrintLoginWindow(Gadget gadget) {
        String str = "";
        if(gadget.getNavMode()==de.tabuma.pm.constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT) {
            ExternalContent ec = ExternalContentFacade.get(gadget.getNavModeElementId());
            ExternalProject ep = ExternalProjectFacade.get(ec.getExternProjectId());
            String loginText = "Login for "+ep.getName();
            String login = "Login";
            int id = ep.getId().intValue();
            List<LoginUserParameter> list = LoginUserParameterFacade.getList(ep.getId().intValue());

            str += "\t\tfunction " + constants.gadget + constants.printLoginWindow + id + "(" + constants.responseFunction + ") {\n";
            str += "\t\t" + constants.gadget + constants.showLoginWindow + id + " = 1;\n";
            str += "\t\t\tstr" + id + "='';\n";
            str += "\t\t\tstr" + id + "+='<p>"+loginText+"</p>';\n";
            str += "\t\t\tstr" + id + "+='<table>';\n";
            for(int i=0;i<list.size();i++) {
                LoginUserParameter lup = list.get(i);
                str += "\t\t\tstr" + id + "+='<tr>';\n";
                str += "\t\t\tstr" + id + "+='<td>" + lup.getNameForUser() + "</td>';\n";
                str += "\t\t\tstr" + id + "+='<td><input type=\"text\" name=\"" + constants.loginVarName + lup.getId() + "\" value=\"\"/></td>';\n";
                str += "\t\t\tstr" + id + "+='</tr>';\n";
            }
            str += "\t\t\tstr" + id + "+='</table>';\n";
            str += "\t\t\tstr" + id + "+='<input type=\"submit\" name=\"Login\" value=\"" + login + "\" onClick=\"javascript:parent.'+" + constants.responseFunction + "+'()\">';\n";
            str += "\t\t\t" + constants.printIFrame + "(str" + id + ");\n";
            str += "\t\t}\n";
        }
        return str;
    }

    public static String getGeneralLoginWindow(Gadget gadget) {
        String str = "";
        if(gadget.getNavMode()==de.tabuma.pm.constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT) {
            ExternalContent ec = ExternalContentFacade.get(gadget.getNavModeElementId());
            ExternalProject ep = ExternalProjectFacade.get(ec.getExternProjectId());
            int id = ep.getId().intValue();
            List<LoginUserParameter> list = LoginUserParameterFacade.getList(ep.getId().intValue());
            str += "\t\tfunction " + constants.gadget + constants.printGenerelLoginResponseFunction + id + "() {\n";
            str += "\t\t\tif("+constants.gadget + constants.madeLoginInProject+ep.getId().intValue()+"==0) {\n";
            for(int i=0;i<list.size();i++) {
                LoginUserParameter lup = list.get(i);
                str += "\t\t\t\tdocument." + constants.gadget + "." + constants.projectForm + ep.getId() + "." + lup.getName() + ".value = parent."+constants.contentIframe+".document.getElementsByName(\"" + constants.loginVarName + lup.getId() + "\")[0].value;\n";
            }
            str += "\t\t\t\t" + constants.gadget + constants.madeLoginInProject + ep.getId() + " = 1;\n";
            str += "\t\t\t}\n";
            str += "\t\t}\n";
        }
        return str;
    }

    public static String getExternalProjectVars(Gadget gadget) {

        String str = "";
        if(gadget.getNavMode()==de.tabuma.pm.constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT) {
            ExternalContent ec = ExternalContentFacade.get(gadget.getNavModeElementId());
            ExternalProject ep = ExternalProjectFacade.get(ec.getExternProjectId());
            str += "\t\tvar " + constants.gadget + constants.varActionTime+ep.getId()+" = 0;\n";
            str += "\t\tvar " + constants.gadget + constants.madeLoginInProject+ep.getId()+" = 0;\n";
            str += "\t\tvar " + constants.gadget + constants.showLoginWindow+ep.getId()+" = 0;\n";
        }
        return str;
    }

    public static String getFormGadget(Gadget gadget) {
        String str = "";
        if(gadget.getNavMode()==de.tabuma.pm.constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT) {
            ExternalContent ec = ExternalContentFacade.get(gadget.getNavModeElementId());
            ExternalProject ep = ExternalProjectFacade.get(ec.getExternProjectId());
            str += "\t\t\t<form name=\"" + constants.gadget + constants.projectForm + ep.getId()+"\" action=\"" + ep.getLoginUrl() + "\" method=\"" + (ep.getPost()==Boolean.TRUE ? "POST" : "GET") + "\" target=\"" + constants.gadget + gadget.getId() + "\">\n";
            List<LoginUserParameter> list = LoginUserParameterFacade.getList(ep.getId().intValue());
            List<HiddenParameter> list1 = HiddenParameterFacade.getList(ep.getId().intValue());
            for(int i=0;i<list.size();i++) {
                LoginUserParameter lup = list.get(i);
                str += "\t\t\t\t<input type=\"hidden\" name=\"" + lup.getName() + "\" value=\"\"/>\n";
            }
            for(int i=0;i<list1.size();i++) {
                HiddenParameter hp = list1.get(i);
                str += "\t\t\t\t<input type=\"hidden\" name=\"" + hp.getName() + "\" value=\"" + hp.getValue() + "\"/>\n";
            }
            str += "\t\t\t</form>\n";
        }
        return str;
    }
    
    public static String getGeneralFunction(Gadget gadget) {
        String str = "";
        switch(gadget.getNavMode()) {
            case de.tabuma.pm.constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT:
                ExternalContent ec = ExternalContentFacade.get(gadget.getNavModeElementId());

                break;
            case de.tabuma.pm.constants.GADGET_ACTION_SETTING_HTML_CONTENT:

                break;
            case de.tabuma.pm.constants.GADGET_ACTION_SETTING_SOAP_CONTENT:
                break;
        }
        return str;
    }

    public static String getLoginFunction(Gadget gadget) {
        String str = "";
        if(gadget.getNavMode()==de.tabuma.pm.constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT) {
            ExternalContent ec = ExternalContentFacade.get(gadget.getNavModeElementId());
            ExternalProject ep = ExternalProjectFacade.get(ec.getExternProjectId());
            int id = ec.getId().intValue();
            str = "\t\tfunction "+constants.gadget+constants.printLoginResponseFunction+id+"() {\n";
            if(ec.getExternProjectId()!=0) {
                String url = ec.getUrl();
                str += "\t\t\t\tdocument.getElementsByName(\""+constants.contentIframe+"\")[0].style.visibility = \"hidden\";\n";
                str += "\t\t\t\tdocument.getElementsByName(\""+constants.contentIframe1+"\")[0].style.visibility = \"hidden\";\n";
                str += "\t\t\tif("+constants.gadget+constants.showLoginWindow+ep.getId()+"==0) {\n";
                str += "\t\t\t\tif("+constants.gadget+constants.madeLoginInProject+ep.getId()+"==0) {\n";
                str += "\t\t\t\t\t"+constants.gadget+constants.printLoginWindow+ep.getId()+"('"+constants.gadget+constants.responseFunction+id+"');\n";
                str += "\t\t\t\t\treturn;\n";
                str += "\t\t\t\t}\n";
                str += "\t\t\t}\n";

                str += "\t\t\t"+constants.printGenerelLoginResponseFunction + ep.getId().intValue() +"();\n";
                str += "\t\t\tvar2 = new Date();\n";
                str += "\t\t\tvar1 = var2.getTime() - "+constants.gadget+constants.varActionTime+ep.getId().intValue()+";\n";
                //is last action less than 5 Minutes ago?
                str += "\t\t\tif(var1 > 300000) {\n";
                //yes
                List<LoginUserParameter> lupList = LoginUserParameterFacade.getList(ep.getId().intValue());
                for(int i=0;i<lupList.size();i++) {
                    LoginUserParameter lup = lupList.get(i);
                    url = url.replaceAll(Pattern.quote("$"+lup.getName()), "'+document." + constants.gadget + constants.projectForm + ep.getId() +"." + lup.getName()+ ".value+'");
                }
                str += "\t\t\t\tdocument."+constants.gadget+constants.projectForm+ep.getId()+".submit();\n";
                str += "\t\t\t\tdocument.getElementById(\""+constants.gadget+constants.loginWaitWindow+"\").style.visibility = \"visible\";\n";
                str += "\t\t\t\twindow.setTimeout(\"document.getElementsByName('"+constants.contentIframe1+"')[0].src='" + url + "';document.getElementById('"+constants.loginWaitWindow+"').style.visibility = 'hidden';\",5000);document.getElementsByName(\""+constants.contentIframe1+"\")[0].style.visibility = \"visible\";\n";
                str += "\t\t\t\tvar2 = new Date();\n";
                str += "\t\t\t\t"+constants.varActionTime + ep.getId().intValue() + " = var2.getTime();\n";
                str += "\t\t\t}\n";
                //no
                str += "\t\t\telse {\n";
                str += "\t\t\t\tvar2 = new Date();\n";
                str += "\t\t\t\t"+constants.varActionTime + ep.getId().intValue() + " = var2.getTime();\n";
                str += "\t\t\t\tdocument.getElementsByName('"+constants.contentIframe1+"')[0].src='" +  url + "';\n";
                str += "\t\t\t\tdocument.getElementsByName(\""+constants.contentIframe1+"\")[0].style.visibility = \"visible\";\n";
                str += "\t\t\t}\n";
            }
            else {
                str += "\t\t\t\tdocument.getElementsByName('"+constants.contentIframe1+"')[0].src='" +  ec.getUrl() + "';\n";
                str += "\t\t\t\tdocument.getElementsByName(\""+constants.contentIframe1+"\")[0].style.visibility = \"visible\";\n";
            }
            str += "\t\t}\n";
        }
        return str;
    }
}
